{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "access_token: 24.bca7590bd52547493748ad1162b6544f.2592000.1613895189.282335-23136973\n"
     ]
    }
   ],
   "source": [
    " # coding=utf-8\n",
    "\n",
    "import sys\n",
    "import json\n",
    "import base64\n",
    "import time\n",
    "import requests \n",
    "from urllib import request,parse\n",
    "def get_token():\n",
    "    \"\"\"\"\"\"\n",
    "    \n",
    "    API_Key = \"A6uFbNXZf6kij6ukqrYDofUx\"            # 官网获取的API_Key\n",
    "    Secret_Key = \"VifzzLdbOotbp2wLtjEnEOzNUEFwiIPz\" # 为官网获取的Secret_Key\n",
    "    #拼接得到Url\n",
    "    Url = \"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=\"+API_Key+\"&client_secret=\"+Secret_Key\n",
    "    try:\n",
    "        resp = request.urlopen(Url)\n",
    "        result = json.loads(resp.read().decode('utf-8'))\n",
    "        # 打印access_token\n",
    "        print(\"access_token:\",result['access_token'])\n",
    "        return result['access_token']\n",
    "    except request.URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "if __name__ == '__main__':\n",
    "    access_token = get_token()\n",
    "    access_token        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关键字提取\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 腾讯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"Keywords\": [{\"Score\": 1.0, \"Word\": \"歧视\"}, {\"Score\": 0.8965953, \"Word\": \"影响\"}, {\"Score\": 0.8072778, \"Word\": \"可能会\"}, {\"Score\": 0.6988214, \"Word\": \"眼光\"}, {\"Score\": 0.6086257, \"Word\": \"异样\"}], \"RequestId\": \"64a9aa8d-77cc-41f0-90d0-fbcbb942eae5\"}\n"
     ]
    }
   ],
   "source": [
    "# 官方自动生成\n",
    "import json\n",
    "from tencentcloud.common import credential\n",
    "from tencentcloud.common.profile.client_profile import ClientProfile\n",
    "from tencentcloud.common.profile.http_profile import HttpProfile\n",
    "from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException\n",
    "from tencentcloud.nlp.v20190408 import nlp_client, models\n",
    "try: \n",
    "    cred = credential.Credential(\"AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx\", \"0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC\") \n",
    "    httpProfile = HttpProfile()\n",
    "    httpProfile.endpoint = \"nlp.tencentcloudapi.com\"\n",
    "\n",
    "    clientProfile = ClientProfile()\n",
    "    clientProfile.httpProfile = httpProfile\n",
    "    client = nlp_client.NlpClient(cred, \"ap-guangzhou\", clientProfile) \n",
    "\n",
    "# #     # read txt\n",
    "#     emb_filename = 'try.txt'\n",
    "#     fr = open(emb_filename,\"r\")\n",
    "#     for line in fr:\n",
    "#         print(line)\n",
    "#     f = open('try.txt')\n",
    "#     f.read()\n",
    "#     data = f.read\n",
    "#     f.close()\n",
    "    with open('try.txt','r',encoding='utf-8') as f:\n",
    "        data = f.readline()\n",
    "    \n",
    "    req = models.KeywordsExtractionRequest()\n",
    "    params = {\n",
    "        \"Text\": data\n",
    "    }\n",
    "\n",
    "    req.from_json_string(json.dumps(params))\n",
    "\n",
    "    resp = client.KeywordsExtraction(req) \n",
    "    print(resp.to_json_string()) \n",
    "\n",
    "except TencentCloudSDKException as err: \n",
    "    print(err) \n",
    "      "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 讯飞"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"code\":\"0\",\"data\":{\"ke\":[{\"score\":\"0.578\",\"word\":\"歧视\"},{\"score\":\"0.563\",\"word\":\"影响\"},{\"score\":\"0.543\",\"word\":\"同性恋\"},{\"score\":\"0.535\",\"word\":\"社交\"},{\"score\":\"0.534\",\"word\":\"偏见\"},{\"score\":\"0.529\",\"word\":\"排斥\"},{\"score\":\"0.528\",\"word\":\"造成\"},{\"score\":\"0.528\",\"word\":\"理解\"},{\"score\":\"0.528\",\"word\":\"排挤\"},{\"score\":\"0.526\",\"word\":\"目光\"}]},\"desc\":\"success\",\"sid\":\"ltp01b3bacb@dxa55d1365ce48000100\"}\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    "import time\n",
    "import urllib.request\n",
    "import urllib.parse\n",
    "import json\n",
    "import hashlib\n",
    "import base64\n",
    "#接口地址\n",
    "url =\"http://ltpapi.xfyun.cn/v1/ke\"\n",
    "#开放平台应用ID\n",
    "x_appid = \"600a7fb3\"\n",
    "#开放平台应用接口秘钥\n",
    "api_key = \"c804d844f9016d03ce50842e53e6ec23\"\n",
    "#语言文本\n",
    "with open('try.txt','r',encoding='utf-8') as f:\n",
    "        data = f.readline()\n",
    "\n",
    "def main():\n",
    "    body = urllib.parse.urlencode({'text': data}).encode('utf-8')\n",
    "    param = {\"type\": \"dependent\"}\n",
    "    x_param = base64.b64encode(json.dumps(param).replace(' ', '').encode('utf-8'))\n",
    "    x_time = str(int(time.time()))\n",
    "    x_checksum = hashlib.md5(api_key.encode('utf-8') + str(x_time).encode('utf-8') + x_param).hexdigest()\n",
    "    x_header = {'X-Appid': x_appid,\n",
    "                'X-CurTime': x_time,\n",
    "                'X-Param': x_param,\n",
    "                'X-CheckSum': x_checksum}\n",
    "    req = urllib.request.Request(url, body, x_header)\n",
    "    result = urllib.request.urlopen(req)\n",
    "    result = result.read()\n",
    "    print(result.decode('utf-8'))\n",
    "    return\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "ConnectionError",
     "evalue": "HTTPConnectionPool(host='nlp.tencentcloudapi.com', port=80): Max retries exceeded with url: /v2?SecretId=AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx&SecretKey=0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC&Action=KeywordsExtraction&Text=%E4%BC%9A%E5%9C%A8%E7%94%9F%E6%B4%BB%E7%9A%84%E4%B8%8A%E6%9C%89%E5%BD%B1%E5%93%8D%E5%90%A7%EF%BC%8C%E4%BC%9A%E6%9C%89%E6%AD%A7%E8%A7%86%E4%BB%80%E4%B9%88%E7%9A%84%E3%80%82&Version=2019-04-08&Region=ap-guangzhou (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000002080A8AD700>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败。'))",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTimeoutError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\connection.py\u001b[0m in \u001b[0;36m_new_conn\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    158\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 159\u001b[1;33m             conn = connection.create_connection(\n\u001b[0m\u001b[0;32m    160\u001b[0m                 \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_dns_host\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mport\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mextra_kw\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\util\\connection.py\u001b[0m in \u001b[0;36mcreate_connection\u001b[1;34m(address, timeout, source_address, socket_options)\u001b[0m\n\u001b[0;32m     83\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0merr\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 84\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     85\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\util\\connection.py\u001b[0m in \u001b[0;36mcreate_connection\u001b[1;34m(address, timeout, source_address, socket_options)\u001b[0m\n\u001b[0;32m     73\u001b[0m                 \u001b[0msock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbind\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msource_address\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 74\u001b[1;33m             \u001b[0msock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msa\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     75\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0msock\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTimeoutError\u001b[0m: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败。",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mNewConnectionError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36murlopen\u001b[1;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)\u001b[0m\n\u001b[0;32m    669\u001b[0m             \u001b[1;31m# Make the request on the httplib connection object.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 670\u001b[1;33m             httplib_response = self._make_request(\n\u001b[0m\u001b[0;32m    671\u001b[0m                 \u001b[0mconn\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36m_make_request\u001b[1;34m(self, conn, method, url, timeout, chunked, **httplib_request_kw)\u001b[0m\n\u001b[0;32m    391\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 392\u001b[1;33m             \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mhttplib_request_kw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    393\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36mrequest\u001b[1;34m(self, method, url, body, headers, encode_chunked)\u001b[0m\n\u001b[0;32m   1239\u001b[0m         \u001b[1;34m\"\"\"Send a complete request to the server.\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1240\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_send_request\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode_chunked\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1241\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36m_send_request\u001b[1;34m(self, method, url, body, headers, encode_chunked)\u001b[0m\n\u001b[0;32m   1285\u001b[0m             \u001b[0mbody\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_encode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'body'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1286\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mendheaders\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode_chunked\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mencode_chunked\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1287\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36mendheaders\u001b[1;34m(self, message_body, encode_chunked)\u001b[0m\n\u001b[0;32m   1234\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mCannotSendHeader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1235\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_send_output\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage_body\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencode_chunked\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mencode_chunked\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1236\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36m_send_output\u001b[1;34m(self, message_body, encode_chunked)\u001b[0m\n\u001b[0;32m   1005\u001b[0m         \u001b[1;32mdel\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_buffer\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1006\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1007\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36msend\u001b[1;34m(self, data)\u001b[0m\n\u001b[0;32m    945\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mauto_open\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 946\u001b[1;33m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    947\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\connection.py\u001b[0m in \u001b[0;36mconnect\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    186\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 187\u001b[1;33m         \u001b[0mconn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_new_conn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    188\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_prepare_conn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mconn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\connection.py\u001b[0m in \u001b[0;36m_new_conn\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    170\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mSocketError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 171\u001b[1;33m             raise NewConnectionError(\n\u001b[0m\u001b[0;32m    172\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"Failed to establish a new connection: %s\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNewConnectionError\u001b[0m: <urllib3.connection.HTTPConnection object at 0x000002080A8AD700>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败。",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mMaxRetryError\u001b[0m                             Traceback (most recent call last)",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\requests\\adapters.py\u001b[0m in \u001b[0;36msend\u001b[1;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[0;32m    438\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mchunked\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 439\u001b[1;33m                 resp = conn.urlopen(\n\u001b[0m\u001b[0;32m    440\u001b[0m                     \u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36murlopen\u001b[1;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)\u001b[0m\n\u001b[0;32m    723\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 724\u001b[1;33m             retries = retries.increment(\n\u001b[0m\u001b[0;32m    725\u001b[0m                 \u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merror\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_pool\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_stacktrace\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexc_info\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\urllib3\\util\\retry.py\u001b[0m in \u001b[0;36mincrement\u001b[1;34m(self, method, url, response, error, _pool, _stacktrace)\u001b[0m\n\u001b[0;32m    438\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mnew_retry\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mis_exhausted\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 439\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mMaxRetryError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_pool\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merror\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mResponseError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcause\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    440\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mMaxRetryError\u001b[0m: HTTPConnectionPool(host='nlp.tencentcloudapi.com', port=80): Max retries exceeded with url: /v2?SecretId=AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx&SecretKey=0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC&Action=KeywordsExtraction&Text=%E4%BC%9A%E5%9C%A8%E7%94%9F%E6%B4%BB%E7%9A%84%E4%B8%8A%E6%9C%89%E5%BD%B1%E5%93%8D%E5%90%A7%EF%BC%8C%E4%BC%9A%E6%9C%89%E6%AD%A7%E8%A7%86%E4%BB%80%E4%B9%88%E7%9A%84%E3%80%82&Version=2019-04-08&Region=ap-guangzhou (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000002080A8AD700>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败。'))",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mConnectionError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-86d9ee05418b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     12\u001b[0m     \u001b[1;34m\"Region\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"ap-guangzhou\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m     }\n\u001b[1;32m---> 14\u001b[1;33m \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpost\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\requests\\api.py\u001b[0m in \u001b[0;36mpost\u001b[1;34m(url, data, json, **kwargs)\u001b[0m\n\u001b[0;32m    117\u001b[0m     \"\"\"\n\u001b[0;32m    118\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 119\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'post'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    120\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    121\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\requests\\api.py\u001b[0m in \u001b[0;36mrequest\u001b[1;34m(method, url, **kwargs)\u001b[0m\n\u001b[0;32m     59\u001b[0m     \u001b[1;31m# cases, and look like a memory leak in others.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     60\u001b[0m     \u001b[1;32mwith\u001b[0m \u001b[0msessions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSession\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 61\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     62\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     63\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\requests\\sessions.py\u001b[0m in \u001b[0;36mrequest\u001b[1;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[0;32m    528\u001b[0m         }\n\u001b[0;32m    529\u001b[0m         \u001b[0msend_kwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msettings\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 530\u001b[1;33m         \u001b[0mresp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprep\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0msend_kwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    531\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    532\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mresp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\requests\\sessions.py\u001b[0m in \u001b[0;36msend\u001b[1;34m(self, request, **kwargs)\u001b[0m\n\u001b[0;32m    641\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    642\u001b[0m         \u001b[1;31m# Send the request\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 643\u001b[1;33m         \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0madapter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    644\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    645\u001b[0m         \u001b[1;31m# Total elapsed time of the request (approximately)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\requests\\adapters.py\u001b[0m in \u001b[0;36msend\u001b[1;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[0;32m    514\u001b[0m                 \u001b[1;32mraise\u001b[0m \u001b[0mSSLError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    515\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 516\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mConnectionError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    517\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    518\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mClosedPoolError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mConnectionError\u001b[0m: HTTPConnectionPool(host='nlp.tencentcloudapi.com', port=80): Max retries exceeded with url: /v2?SecretId=AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx&SecretKey=0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC&Action=KeywordsExtraction&Text=%E4%BC%9A%E5%9C%A8%E7%94%9F%E6%B4%BB%E7%9A%84%E4%B8%8A%E6%9C%89%E5%BD%B1%E5%93%8D%E5%90%A7%EF%BC%8C%E4%BC%9A%E6%9C%89%E6%AD%A7%E8%A7%86%E4%BB%80%E4%B9%88%E7%9A%84%E3%80%82&Version=2019-04-08&Region=ap-guangzhou (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000002080A8AD700>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败。'))"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "url = \"http://nlp.tencentcloudapi.com/v2?\"\n",
    "\n",
    "params = {\n",
    "    \"SecretId\":\"AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx\",\n",
    "    \"SecretKey\":\"0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC\",\n",
    "    \"Action\":\"KeywordsExtraction\",\n",
    "     \"Text\": \"会在生活的上有影响吧，会有歧视什么的。\",\n",
    "     \n",
    "    \"Version\":\"2019-04-08\",\n",
    "    \"Region\":\"ap-guangzhou\",\n",
    "    }\n",
    "r = requests.post(url,params=params)\n",
    "result = r.json()\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Response': {'Error': {'Code': 'MissingParameter',\n",
       "   'Message': 'The request is missing a required parameter `Timestamp`.'},\n",
       "  'RequestId': '0029f73f-8a4f-4fe8-a276-97d2228ef9b3'}}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "secret_id=\"AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx\",\n",
    "secret_key=\"0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC\",\n",
    "\n",
    "url = \"nlp.tencentcloudapi.com/v2/index.php\"\n",
    "\n",
    "\n",
    "data = {\n",
    "    \"Action\":\"KeywordsExtraction\",    \n",
    "    \"Version\":\"2019-04-08\",\n",
    "    \"Region\":\"ap-guangzhou\",\n",
    "    \"Text\": \"会在生活的上有影响吧，会有歧视什么的。\",\n",
    "}\n",
    "\n",
    "signature_old= ''\n",
    "for i in sorted(data):\n",
    "    \n",
    "    signature_old = signature_old + i + \"=\" + str(data[i]) + \"&\"\n",
    "signature_old =signature_old[:-1]\n",
    "query = 'POST' + url + '?' +signature_old\n",
    "\n",
    "\n",
    "# hmac_str= hmac.new(secret_key.encode('utf8'), query.encode('utf8'), hashlib.sha1).digest()\n",
    "# signature = base64.b64encode(hmac_str)\n",
    "\n",
    "# data[\"Signature\"] = signature\n",
    "\n",
    "r = requests.post('https://' +url,data=data)\n",
    "\n",
    "result = r.json()\n",
    "result\n",
    "\n",
    "# r = requests.post(url=url,params=params)\n",
    "# result = r.json()\n",
    "# result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 情感分析\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 讯飞"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"code\":\"0\",\"data\":{\"score\":0.7639,\"sentiment\":1},\"desc\":\"success\",\"sid\":\"ltp01b42a14@dxa55d1365d5ad000100\"}\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    "import time\n",
    "import urllib.request\n",
    "import urllib.parse\n",
    "import json\n",
    "import hashlib\n",
    "import base64\n",
    "#接口地址\n",
    "url =\"http://ltpapi.xfyun.cn/v2/sa\"\n",
    "#开放平台应用ID\n",
    "x_appid = \"600a7fb3\"\n",
    "#开放平台应用接口秘钥\n",
    "api_key = \"c804d844f9016d03ce50842e53e6ec23\"\n",
    "#语言文本\n",
    "TEXT=\"目前国内对于同性恋的接受度较之前确实有一些改善，但是纵观整个社会和同性恋群体，他们被真正的接受或许还会有些时日无无性格他们只是刚好爱上了个人，而那个人刚好是同性而已\"\n",
    "\n",
    "\n",
    "def main():\n",
    "    body = urllib.parse.urlencode({'text': TEXT}).encode('utf-8')\n",
    "    param = {\"type\": \"dependent\"}\n",
    "    x_param = base64.b64encode(json.dumps(param).replace(' ', '').encode('utf-8'))\n",
    "    x_time = str(int(time.time()))\n",
    "    x_checksum = hashlib.md5(api_key.encode('utf-8') + str(x_time).encode('utf-8') + x_param).hexdigest()\n",
    "    x_header = {'X-Appid': x_appid,\n",
    "                'X-CurTime': x_time,\n",
    "                'X-Param': x_param,\n",
    "                'X-CheckSum': x_checksum}\n",
    "    req = urllib.request.Request(url, body, x_header)\n",
    "    result = urllib.request.urlopen(req)\n",
    "    result = result.read()\n",
    "    print(result.decode('utf-8'))\n",
    "    return\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 百度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def baidu_sentiment(text):\n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?access_token=\"+access_token\n",
    "    data = {\n",
    "        \"text\":text\n",
    "    }\n",
    "    # unicode 国际编码识别中文\n",
    "    encode_data=json.dumps(data).encode('UTF-8')\n",
    "    headers = {\n",
    "        'Content-Type':'application/json'\n",
    "    }\n",
    "    return requests.post(url,encode_data).content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'log_id': 2661860719487772406,\n",
       " 'text': '目前国内对于同性恋的接受度较之前确实有一些改善，但是纵观整个社会和同性恋群体，他们被真正的接受或许还会有些时日无无性格他们只是刚好爱上了个人，而那个人刚好是同性而已',\n",
       " 'items': [{'positive_prob': 0.884251,\n",
       "   'confidence': 0.742779,\n",
       "   'negative_prob': 0.115749,\n",
       "   'sentiment': 2}]}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = baidu_sentiment(text=\"目前国内对于同性恋的接受度较之前确实有一些改善，但是纵观整个社会和同性恋群体，他们被真正的接受或许还会有些时日无无性格他们只是刚好爱上了个人，而那个人刚好是同性而已\")\n",
    "resultgbk = str(result,encoding=\"GBK\")\n",
    "resultdict =json.loads(resultgbk)\n",
    "resultdict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def baidu_emotion(text,scene=\"\"):\n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/nlp/v1/emotion?access_token=\"+'24.5a5b374797755ba8df171779666fbef5.2592000.1612625664.282335-23019635'\n",
    "    data = {\n",
    "        \"text\":text,\n",
    "        \"scene\":scene\n",
    "    }\n",
    "    # unicode 国际编码识别中文\n",
    "    encode_data=json.dumps(data).encode('UTF-8')\n",
    "    headers = {\n",
    "        'Content-Type':'application/json'\n",
    "    }\n",
    "    return requests.post(url,encode_data).content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'log_id': 1401748937036961910,\n",
       " 'text': '目前国内对于同性恋的接受度较之前确实有一些改善，但是纵观整个社会和同性恋群体，他们被真正的接受或许还会有些时日无无性格他们只是刚好爱上了个人，而那个人刚好是同性而已',\n",
       " 'items': [{'subitems': [{'prob': 0.901738, 'label': 'angry'}],\n",
       "   'replies': ['对不起，我会继续努力'],\n",
       "   'prob': 0.995139,\n",
       "   'label': 'pessimistic'},\n",
       "  {'subitems': [], 'replies': [], 'prob': 0.00486143, 'label': 'neutral'},\n",
       "  {'subitems': [], 'replies': [], 'prob': 0, 'label': 'optimistic'}]}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result07 = baidu_emotion(text=\"目前国内对于同性恋的接受度较之前确实有一些改善，但是纵观整个社会和同性恋群体，他们被真正的接受或许还会有些时日无无性格他们只是刚好爱上了个人，而那个人刚好是同性而已\",scene=\"default\")\n",
    "result07gbk = str(result07,encoding=\"GBK\")\n",
    "result07dict =json.loads(result07gbk)\n",
    "result07dict "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 腾讯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "-------------------------------------------------\n",
    "   File Name：     md5sign\n",
    "   Description :\n",
    "   Author :       YOUQING\n",
    "   date：          2017/11/20\n",
    "-------------------------------------------------\n",
    "   Change Activity:\n",
    "                   2017/11/20:\n",
    "-------------------------------------------------\n",
    "\"\"\"\n",
    "import hashlib\n",
    "import time\n",
    "import random\n",
    "import string\n",
    "import urllib\n",
    "import sys\n",
    "\n",
    "\n",
    "def get_params(plus_item):\n",
    "    '''请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效）'''\n",
    "    t = time.time()\n",
    "    time_stamp=int(t)\n",
    "\n",
    "    '''请求随机字符串，用于保证签名不可预测'''\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))\n",
    "\n",
    "    '''应用标志，这里修改成自己的id和key'''\n",
    "    app_id='XXXXXX'\n",
    "    app_key='XXXXXXXXXXXX'\n",
    "\n",
    "    '''值使用URL编码，URL编码算法用大写字母'''\n",
    "    text1=plus_item\n",
    "    text=urllib.quote(text1.decode(sys.stdin.encoding).encode('utf8')).upper()\n",
    "\n",
    "    '''拼接应用密钥，得到字符串S'''\n",
    "    sign_before='app_id='+app_id+'&nonce_str='+nonce_str+'&text='+text+'&time_stamp='+str(time_stamp)+'&app_key='+app_key\n",
    "\n",
    "    '''计算MD5摘要，得到签名字符串'''\n",
    "    m=hashlib.md5()\n",
    "    m.update(sign_before)\n",
    "    sign=m.hexdigest()\n",
    "    sign=sign.upper()\n",
    "\n",
    "    params='app_id='+app_id+'&time_stamp='+str(time_stamp)+'&nonce_str='+nonce_str+'&sign='+sign+'&text='+text\n",
    "\n",
    "    return params\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "import requests\n",
    "import md5sign\n",
    "from bs4 import BeautifulSoup\n",
    "import json\n",
    "import sys\n",
    "\n",
    "reload(sys)\n",
    "sys.setdefaultencoding('utf-8')\n",
    "\n",
    "\n",
    "def get_content(plus_item):\n",
    "    url = \"https://api.ai.qq.com/fcgi-bin/nlp/nlp_textpolar\"  # API地址\n",
    "    params = md5sign.get_params(plus_item)#获取请求参数\n",
    "    url=url+'?'+params#请求地址拼接\n",
    "    try:\n",
    "        r = requests.get(url)\n",
    "        soup = BeautifulSoup(r.text, 'lxml')\n",
    "        allcontents=soup.select('body')[0].text.strip()\n",
    "        allcontents_json=json.loads(allcontents)#str转成dict\n",
    "\n",
    "        return allcontents_json[\"data\"][\"polar\"],allcontents_json[\"data\"][\"confd\"],allcontents_json[\"data\"][\"text\"]\n",
    "    except Exception, e:\n",
    "        print 'a', str(e)\n",
    "        return 0,0,0\n",
    "\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    polar,confd,text=get_content('今天天气真好')\n",
    "    print '情感倾向：'+str(polar)+'\\n'+'程度：'+str(confd)+'\\n'+'文本：'+text"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 语音识别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 百度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "API_Key = \"TGsUxQBpbbZtO1kyVzjAv0pk\"            # 官网获取的API_Key\n",
    "Secret_Key = \"XiIcglDvy4EGabFpoE1FtNNkrInOTzKO\" # 为官网获取的Secret_Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"access_token\":\"24.3095194b231fd2e2c61fc1553b6b0932.2592000.1613897518.282335-15803531\",\"session_key\":\"9mzdCPR3yE9ezV5TjRcT8134p1X+2\\/vbVJCabQBOSlY8uF7hecDfT7T0rIlfy1CgXC9Q4\\/Zzb8sFmIF1qqyHNrQqYqegJQ==\",\"scope\":\"brain_asr_async brain_speech_realtime vis-faceverify_FACE_Police brain_enhanced_asr audio_voice_assistant_get audio_tts_post public brain_all_scope wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin\",\"refresh_token\":\"25.be69032bda2a2008f7b5eba289b9d3a0.315360000.1926665518.282335-15803531\",\"session_secret\":\"87382b3786cf037840cc5c0db1d7be82\",\"expires_in\":2592000}\n",
      "\n",
      "{'access_token': '24.3095194b231fd2e2c61fc1553b6b0932.2592000.1613897518.282335-15803531', 'session_key': '9mzdCPR3yE9ezV5TjRcT8134p1X+2/vbVJCabQBOSlY8uF7hecDfT7T0rIlfy1CgXC9Q4/Zzb8sFmIF1qqyHNrQqYqegJQ==', 'scope': 'brain_asr_async brain_speech_realtime vis-faceverify_FACE_Police brain_enhanced_asr audio_voice_assistant_get audio_tts_post public brain_all_scope wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin', 'refresh_token': '25.be69032bda2a2008f7b5eba289b9d3a0.315360000.1926665518.282335-15803531', 'session_secret': '87382b3786cf037840cc5c0db1d7be82', 'expires_in': 2592000}\n",
      "SUCCESS WITH TOKEN: 24.3095194b231fd2e2c61fc1553b6b0932.2592000.1613897518.282335-15803531 ; EXPIRES IN SECONDS: 2592000\n",
      "url is http://vop.baidu.com/server_api?cuid=123456PYTHON&token=24.3095194b231fd2e2c61fc1553b6b0932.2592000.1613897518.282335-15803531&dev_pid=1537\n",
      "header is {'Content-Type': 'audio/m4a; rate=16000', 'Content-Length': 212348}\n",
      "Request time cost 1.475484\n",
      "{\"corpus_no\":\"6920504504792614896\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"苏州有个苏胡子，湖州有个胡梳子胡梳子买把斧子做梳子，苏胡子买把梳子子。\"],\"sn\":\"575313243731611305518\"}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# A-1\n",
    "\n",
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "\n",
    "    timer = time.perf_counter\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "\n",
    "    if sys.platform == \"win32\":\n",
    "        timer = time.clock\n",
    "    else:\n",
    "        # On most other platforms the best timer is time.time()\n",
    "        timer = time.time\n",
    "\n",
    "API_KEY = 'kVcnfD9iW2XVZSMaLMrtLYIz'\n",
    "SECRET_KEY = 'O9o1O213UgG5LFn0bDGNtoRN3VWl2du6'\n",
    "\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = './gas_reading.m4a' # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "#测试自训练平台需要打开以下信息， 自训练平台模型上线后，您会看见 第二步：“”获取专属模型参数pid:8001，modelid:1234”，按照这个信息获取 dev_pid=8001，lm_id=1234\n",
    "# DEV_PID = 8001 ;   \n",
    "# LM_ID = 1234 ;\n",
    "\n",
    "# 极速版 打开注释的话请填写自己申请的appkey appSecret ，并在网页中开通极速版（开通后可能会收费）\n",
    "\n",
    "#DEV_PID = 80001\n",
    "#ASR_URL = 'http://vop.baidu.com/pro_api'\n",
    "#SCOPE = 'brain_enhanced_asr'  # 有此scope表示有asr能力，没有请在网页里开通极速版\n",
    "\n",
    "# 忽略scope检查，非常旧的应用可能没有\n",
    "# SCOPE = False\n",
    "\n",
    "\n",
    "# 极速版\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "\n",
    "TOKEN_URL = 'http://openapi.baidu.com/oauth/2.0/token'\n",
    "\n",
    "\n",
    "def fetch_token():\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "    print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "    print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if SCOPE and (not SCOPE in result['scope'].split(' ')):  # SCOPE = False 忽略检查\n",
    "            raise DemoError('scope is not correct')\n",
    "        print('SUCCESS WITH TOKEN: %s ; EXPIRES IN SECONDS: %s' % (result['access_token'], result['expires_in']))\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    token = fetch_token()\n",
    "\n",
    "    \"\"\"\n",
    "    httpHandler = urllib2.HTTPHandler(debuglevel=1)\n",
    "    opener = urllib2.build_opener(httpHandler)\n",
    "    urllib2.install_opener(opener)\n",
    "    \"\"\"\n",
    "\n",
    "    speech_data = []\n",
    "    with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "        speech_data = speech_file.read()\n",
    "    length = len(speech_data)\n",
    "    if length == 0:\n",
    "        raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "    params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID}\n",
    "    #测试自训练平台需要打开以下信息\n",
    "    #params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "    params_query = urlencode(params);\n",
    "\n",
    "    headers = {\n",
    "        'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "        'Content-Length': length\n",
    "    }\n",
    "\n",
    "    url = ASR_URL + \"?\" + params_query\n",
    "    print(\"url is\", url);\n",
    "    print(\"header is\", headers)\n",
    "    # print post_data\n",
    "    req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "    try:\n",
    "        begin = timer()\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "        print(\"Request time cost %f\" % (timer() - begin))\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "\n",
    "    if (IS_PY3):\n",
    "        result_str = str(result_str, 'utf-8')\n",
    "    print(result_str)\n",
    "    with open(\"result.txt\", \"w\") as of:\n",
    "        of.write(result_str)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 讯飞"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'websocket' has no attribute 'WebSocketApp'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-40-cd1dda33c4fe>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m    174\u001b[0m \u001b[1;31m#     websocket.enableTrace(False)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    175\u001b[0m     \u001b[0mwsUrl\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwsParam\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcreate_url\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 176\u001b[1;33m     \u001b[0mws\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwebsocket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mWebSocketApp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwsUrl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mon_message\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mon_message\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mon_error\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mon_error\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mon_close\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mon_close\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    177\u001b[0m     \u001b[0mws\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mon_open\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mon_open\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    178\u001b[0m     \u001b[0mws\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun_forever\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msslopt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m\"cert_reqs\"\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mssl\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mCERT_NONE\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'websocket' has no attribute 'WebSocketApp'"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "#\n",
    "#   author: iflytek\n",
    "#\n",
    "#  本demo测试时运行的环境为：Windows + Python3.7\n",
    "#  本demo测试成功运行时所安装的第三方库及其版本如下，您可自行逐一或者复制到一个新的txt文件利用pip一次性安装：\n",
    "#   cffi==1.12.3\n",
    "#   gevent==1.4.0\n",
    "#   greenlet==0.4.15\n",
    "#   pycparser==2.19\n",
    "#   six==1.12.0\n",
    "#   websocket==0.2.1\n",
    "#   websocket-client==0.56.0\n",
    "#\n",
    "#  语音听写流式 WebAPI 接口调用示例 接口文档（必看）：https://doc.xfyun.cn/rest_api/语音听写（流式版）.html\n",
    "#  webapi 听写服务参考帖子（必看）：http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=38947&extra=\n",
    "#  语音听写流式WebAPI 服务，热词使用方式：登陆开放平台https://www.xfyun.cn/后，找到控制台--我的应用---语音听写（流式）---服务管理--个性化热词，\n",
    "#  设置热词\n",
    "#  注意：热词只能在识别的时候会增加热词的识别权重，需要注意的是增加相应词条的识别率，但并不是绝对的，具体效果以您测试为准。\n",
    "#  语音听写流式WebAPI 服务，方言试用方法：登陆开放平台https://www.xfyun.cn/后，找到控制台--我的应用---语音听写（流式）---服务管理--识别语种列表\n",
    "#  可添加语种或方言，添加后会显示该方言的参数值\n",
    "#  错误码链接：https://www.xfyun.cn/document/error-code （code返回错误码时必看）\n",
    "import websocket\n",
    "import datetime\n",
    "import hashlib\n",
    "import base64\n",
    "import hmac\n",
    "import json\n",
    "from urllib.parse import urlencode\n",
    "import time\n",
    "import ssl\n",
    "from wsgiref.handlers import format_date_time\n",
    "from datetime import datetime\n",
    "from time import mktime\n",
    "import _thread as thread\n",
    "\n",
    "STATUS_FIRST_FRAME = 0  # 第一帧的标识\n",
    "STATUS_CONTINUE_FRAME = 1  # 中间帧标识\n",
    "STATUS_LAST_FRAME = 2  # 最后一帧的标识\n",
    "\n",
    "\n",
    "class Ws_Param(object):\n",
    "    # 初始化\n",
    "    def __init__(self, APPID, APIKey, APISecret, AudioFile):\n",
    "        self.APPID = APPID\n",
    "        self.APIKey = APIKey\n",
    "        self.APISecret = APISecret\n",
    "        self.AudioFile = AudioFile\n",
    "\n",
    "        # 公共参数(common)\n",
    "        self.CommonArgs = {\"app_id\": self.APPID}\n",
    "        # 业务参数(business)，更多个性化参数可在官网查看\n",
    "        self.BusinessArgs = {\"domain\": \"iat\", \"language\": \"zh_cn\", \"accent\": \"mandarin\", \"vinfo\":1,\"vad_eos\":10000}\n",
    "\n",
    "    # 生成url\n",
    "    def create_url(self):\n",
    "        url = 'wss://ws-api.xfyun.cn/v2/iat'\n",
    "        # 生成RFC1123格式的时间戳\n",
    "        now = datetime.now()\n",
    "        date = format_date_time(mktime(now.timetuple()))\n",
    "\n",
    "        # 拼接字符串\n",
    "        signature_origin = \"host: \" + \"ws-api.xfyun.cn\" + \"\\n\"\n",
    "        signature_origin += \"date: \" + date + \"\\n\"\n",
    "        signature_origin += \"GET \" + \"/v2/iat \" + \"HTTP/1.1\"\n",
    "        # 进行hmac-sha256进行加密\n",
    "        signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),\n",
    "                                 digestmod=hashlib.sha256).digest()\n",
    "        signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')\n",
    "\n",
    "        authorization_origin = \"api_key=\\\"%s\\\", algorithm=\\\"%s\\\", headers=\\\"%s\\\", signature=\\\"%s\\\"\" % (\n",
    "            self.APIKey, \"hmac-sha256\", \"host date request-line\", signature_sha)\n",
    "        authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')\n",
    "        # 将请求的鉴权参数组合为字典\n",
    "        v = {\n",
    "            \"authorization\": authorization,\n",
    "            \"date\": date,\n",
    "            \"host\": \"ws-api.xfyun.cn\"\n",
    "        }\n",
    "        # 拼接鉴权参数，生成url\n",
    "        url = url + '?' + urlencode(v)\n",
    "        # print(\"date: \",date)\n",
    "        # print(\"v: \",v)\n",
    "        # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释，比对相同参数时生成的url与自己代码生成的url是否一致\n",
    "        # print('websocket url :', url)\n",
    "        return url\n",
    "\n",
    "\n",
    "# 收到websocket消息的处理\n",
    "def on_message(ws, message):\n",
    "    try:\n",
    "        code = json.loads(message)[\"code\"]\n",
    "        sid = json.loads(message)[\"sid\"]\n",
    "        if code != 0:\n",
    "            errMsg = json.loads(message)[\"message\"]\n",
    "            print(\"sid:%s call error:%s code is:%s\" % (sid, errMsg, code))\n",
    "\n",
    "        else:\n",
    "            data = json.loads(message)[\"data\"][\"result\"][\"ws\"]\n",
    "            # print(json.loads(message))\n",
    "            result = \"\"\n",
    "            for i in data:\n",
    "                for w in i[\"cw\"]:\n",
    "                    result += w[\"w\"]\n",
    "            print(\"sid:%s call success!,data is:%s\" % (sid, json.dumps(data, ensure_ascii=False)))\n",
    "    except Exception as e:\n",
    "        print(\"receive msg,but parse exception:\", e)\n",
    "\n",
    "\n",
    "\n",
    "# 收到websocket错误的处理\n",
    "def on_error(ws, error):\n",
    "    print(\"### error:\", error)\n",
    "\n",
    "\n",
    "# 收到websocket关闭的处理\n",
    "def on_close(ws):\n",
    "    print(\"### closed ###\")\n",
    "\n",
    "\n",
    "# 收到websocket连接建立的处理\n",
    "def on_open(ws):\n",
    "    def run(*args):\n",
    "        frameSize = 8000  # 每一帧的音频大小\n",
    "        intervel = 0.04  # 发送音频间隔(单位:s)\n",
    "        status = STATUS_FIRST_FRAME  # 音频的状态信息，标识音频是第一帧，还是中间帧、最后一帧\n",
    "\n",
    "        with open(wsParam.AudioFile, \"rb\") as fp:\n",
    "            while True:\n",
    "                buf = fp.read(frameSize)\n",
    "                # 文件结束\n",
    "                if not buf:\n",
    "                    status = STATUS_LAST_FRAME\n",
    "                # 第一帧处理\n",
    "                # 发送第一帧音频，带business 参数\n",
    "                # appid 必须带上，只需第一帧发送\n",
    "                if status == STATUS_FIRST_FRAME:\n",
    "\n",
    "                    d = {\"common\": wsParam.CommonArgs,\n",
    "                         \"business\": wsParam.BusinessArgs,\n",
    "                         \"data\": {\"status\": 0, \"format\": \"audio/L16;rate=16000\",\n",
    "                                  \"audio\": str(base64.b64encode(buf), 'utf-8'),\n",
    "                                  \"encoding\": \"raw\"}}\n",
    "                    d = json.dumps(d)\n",
    "                    ws.send(d)\n",
    "                    status = STATUS_CONTINUE_FRAME\n",
    "                # 中间帧处理\n",
    "                elif status == STATUS_CONTINUE_FRAME:\n",
    "                    d = {\"data\": {\"status\": 1, \"format\": \"audio/L16;rate=16000\",\n",
    "                                  \"audio\": str(base64.b64encode(buf), 'utf-8'),\n",
    "                                  \"encoding\": \"raw\"}}\n",
    "                    ws.send(json.dumps(d))\n",
    "                # 最后一帧处理\n",
    "                elif status == STATUS_LAST_FRAME:\n",
    "                    d = {\"data\": {\"status\": 2, \"format\": \"audio/L16;rate=16000\",\n",
    "                                  \"audio\": str(base64.b64encode(buf), 'utf-8'),\n",
    "                                  \"encoding\": \"raw\"}}\n",
    "                    ws.send(json.dumps(d))\n",
    "                    time.sleep(1)\n",
    "                    break\n",
    "                # 模拟音频采样间隔\n",
    "                time.sleep(intervel)\n",
    "        ws.close()\n",
    "\n",
    "    thread.start_new_thread(run, ())\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 测试时候在此处正确填写相关信息即可运行\n",
    "    time1 = datetime.now()\n",
    "    wsParam = Ws_Param(APPID='600a7fb3 ', APISecret='1ba4d8e21df04cbcf6d00f34b1d24325 ',\n",
    "                       APIKey='5523685f23f5e6c32c061cad0b75c0a3 ',\n",
    "                       AudioFile=r'gas_reading.m4a')\n",
    "    websocket.enableTrace(False)\n",
    "    wsUrl = wsParam.create_url()\n",
    "    ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close)\n",
    "    ws.on_open = on_open\n",
    "    ws.run_forever(sslopt={\"cert_reqs\": ssl.CERT_NONE})\n",
    "    time2 = datetime.now()\n",
    "    print(time2-time1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 腾讯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"Result\": \"Zhu. \", \"AudioDuration\": 5524, \"WordSize\": 0, \"WordList\": null, \"RequestId\": \"9461cdc4-805c-450a-adfe-0341e8253b55\"}'\n"
     ]
    }
   ],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "import json\n",
    "from urllib import request\n",
    "from tencentcloud.common import credential\n",
    "from tencentcloud.common.profile.client_profile import ClientProfile\n",
    "from tencentcloud.common.profile.http_profile import HttpProfile\n",
    "from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException\n",
    "from tencentcloud.asr.v20190614 import asr_client, models\n",
    "import base64\n",
    "\n",
    "#通过本地语音上传方式调用\n",
    "try:\n",
    "\n",
    "    #重要：<Your SecretId>、<Your SecretKey>需要替换成用户自己的账号信息\n",
    "    #请参考接口说明中的使用步骤1进行获取。\n",
    "    secret_key = '0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC'\n",
    "    secretid = 'AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx'\n",
    "    appid = '1303880846'\n",
    "    cred = credential.Credential(\"AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx\", \"0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC\")\n",
    "    httpProfile = HttpProfile()\n",
    "    httpProfile.endpoint = \"asr.tencentcloudapi.com\"\n",
    "    clientProfile = ClientProfile()\n",
    "    clientProfile.httpProfile = httpProfile\n",
    "    clientProfile.signMethod = \"TC3-HMAC-SHA256\"\n",
    "    client = asr_client.AsrClient(cred, \"ap-shanghai\", clientProfile)\n",
    "\n",
    "    #读取文件以及base64\n",
    "#     fwave = open('gas_reading.m4a', mode='rb').read()\n",
    "#     dataLen = len(fwave)\n",
    "#     base64Wav = base64.b64encode(fwave).decode('utf8')\n",
    "    with open(\"gas_reading.m4a\", 'rb') as f:\n",
    "        data = f.read()\n",
    "    base64Wav = base64.b64encode(data).decode()\n",
    "#     print(base64Wav)\n",
    "    #发送请求\n",
    "    req = models.SentenceRecognitionRequest()\n",
    "    params = {\"ProjectId\":0,\"SubServiceType\":2,\"EngSerViceType\":\"16k_en\",\"SourceType\":1,\"Url\":\"\",\"VoiceFormat\":\"m4a\",\"UsrAudioKey\":\"session-123\", \"Data\":base64Wav, \"DataLen\":dataLen}\n",
    "    req._deserialize(params)\n",
    "    resp = client.SentenceRecognition(req)\n",
    "#     print(resp)\n",
    "    print(resp.to_json_string().encode('GBK') )\n",
    "except TencentCloudSDKException as err:\n",
    "    print(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"Result\": \"106\\xce\\xac\\xb6\\xc8\\xb2\\xa9\\xce\\xc4\\xa1\\xa3110\\xce\\xac\\xb6\\xc8\\xd0\\xa1\\xcd\\xaf\\xa1\\xa3\", \"AudioDuration\": 5076, \"WordSize\": 0, \"WordList\": null, \"RequestId\": \"0f723a62-af3c-4c09-804e-20fd624a1121\"}'\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "@author: Looking\n",
    "@email: 2392863668@qq.com\n",
    "\"\"\"\n",
    "import json\n",
    "from urllib import request\n",
    "from tencentcloud.common import credential\n",
    "from tencentcloud.common.profile.client_profile import ClientProfile\n",
    "from tencentcloud.common.profile.http_profile import HttpProfile\n",
    "from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException\n",
    "from tencentcloud.asr.v20190614 import asr_client, models\n",
    "import base64\n",
    " \n",
    "# 通过本地语音上传方式调用\n",
    "try:\n",
    " \n",
    "    # 重要：<Your SecretId>、<Your SecretKey>需要替换成用户自己的账号信息\n",
    "    # 请参考接口说明中的使用步骤1进行获取。\n",
    "    cred = credential.Credential(\"AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx\", \"0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC\")\n",
    "    httpProfile = HttpProfile()\n",
    "    httpProfile.endpoint = \"asr.tencentcloudapi.com\"\n",
    "    clientProfile = ClientProfile()\n",
    "    clientProfile.httpProfile = httpProfile\n",
    "    clientProfile.signMethod = \"TC3-HMAC-SHA256\"\n",
    "    client = asr_client.AsrClient(cred, \"ap-shanghai\", clientProfile)\n",
    " \n",
    "    # 读取文件以及base64\n",
    "    with open(\"./1result.mp3\",'rb') as f:\n",
    "        data = f.read()\n",
    "    base64Wav = base64.b64encode(data).decode()\n",
    " \n",
    "    # 发送请求\n",
    "    req = models.SentenceRecognitionRequest()\n",
    "    params = {\"ProjectId\": 0, \"SubServiceType\": 2, \"EngSerViceType\": \"8k\", \"SourceType\": 1, \"Url\": \"\",\n",
    "              \"VoiceFormat\": \"mp3\", \"UsrAudioKey\": \"session-123\", \"Data\": base64Wav}\n",
    "    req._deserialize(params)\n",
    "    resp = client.SentenceRecognition(req)\n",
    "#     print(resp.to_json_string())\n",
    "    # windows系统使用下面一行替换上面一行\n",
    "    print(resp.to_json_string().encode('GBK') )\n",
    "except TencentCloudSDKException as err:\n",
    "    print(err)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Response': {'Error': {'Code': 'MissingParameter',\n",
       "   'Message': 'The request is missing a required parameter `Action`.'},\n",
       "  'RequestId': 'fccbf618-1ba7-4f81-8f25-9b6b32714b0a'}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "secret_id=\"AKIDaIZZ9udLY80GuOvYFgkX4rNcaKKrBzpx\",\n",
    "secret_key=\"0j9JCo9EUUO1OlrVuW3JHma8PPAh14rC\",\n",
    "\n",
    "url = \"https://nlp.tencentcloudapi.com/v2/index.php\"\n",
    "\n",
    "\n",
    "params = {\n",
    "    \"Action\":\"SentimentAnalysis\",    \n",
    "    \"Version\":\"2019-04-08\",\n",
    "    \"Region\":\"ap-guangzhou\",\n",
    "    \"Text\": \"会在生活的上有影响吧，会有歧视什么的。\",\n",
    "}\n",
    "r = requests.post(url=url,params=params)\n",
    "result = r.json()\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hashlib\n",
    "import time\n",
    "import random\n",
    "import string\n",
    "import urllib\n",
    "import sys\n",
    "    \n",
    "    \n",
    "def get_params(plus_item):\n",
    "    '''请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效）'''\n",
    "    t = time.time()\n",
    "    time_stamp=int(t)\n",
    "    \n",
    "    '''请求随机字符串，用于保证签名不可预测'''\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))\n",
    "    \n",
    "    '''应用标志，这里修改成自己的id和key'''\n",
    "    app_id='2163289962'\n",
    "    app_key='GOOQaojWonHWdsdl'\n",
    "    \n",
    "    '''值使用URL编码，URL编码算法用大写字母'''\n",
    "    text1=plus_item\n",
    "    text=urllib.request.quote(text1.encode('utf-8')).upper()\n",
    "    \n",
    "    '''拼接应用密钥，得到字符串S'''\n",
    "    sign_before='app_id='+app_id+'&nonce_str='+nonce_str+'&text='+text+'&time_stamp='+str(time_stamp)+'&app_key='+app_key\n",
    "    \n",
    "    '''计算MD5摘要，得到签名字符串'''\n",
    "    \n",
    "    sign=hashlib.md5(sign_before.encode('utf-8')).hexdigest()\n",
    "    sign=sign.upper()\n",
    "    \n",
    "    params='app_id='+app_id+'&time_stamp='+str(time_stamp)+'&nonce_str='+nonce_str+'&sign='+sign+'&text='+text\n",
    "    \n",
    "    return params\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "248px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
